package net.huashitong.synthetical.TaskJob.Service;

import com.sirdc.modules.sys.service.RedisService;
import net.huashitong.synthetical.TaskJob.Entity.TaskJob;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ExportService {

    @Autowired
    private TaskJobService taskJobService;
    @Autowired
    private RedisService redisService;

    public HSSFWorkbook getJobListData(List<String> heads ) {
        HSSFWorkbook wb = new HSSFWorkbook();//创建一个对应Excel文件
        List<TaskJob> dataList = taskJobService.query();


        HSSFSheet sheet = wb.createSheet("任务清单"); //床建excel 中的sheet
        HSSFRow row = sheet.createRow(0);
        sheet.autoSizeColumn(1, true);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中格式
        style.setFillForegroundColor((short) 13);// 设置背景色
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //row.setHeight((short) 900);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //================================================================================
        // -------- 列 ---------
        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("序号");
        cell1.setCellStyle(style);
        sheet.setColumnWidth(0, 15 * 112);
       for(int i =0;i<heads.size();i++)
       {
           HSSFCell cell= row.createCell(i+1);
           cell.setCellValue(redisService.get(heads.get(i)) );
           cell.setCellStyle(style);
           sheet.setColumnWidth(i+1, 50 * 112);
       }
       /*

        //------列-------
        CellRangeAddress cellRangeAddress0 = new CellRangeAddress(1, 1, 0, 0);
        sheet.addMergedRegion(cellRangeAddress0);
        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("序号");
        cell1.setCellStyle(style);
        sheet.setColumnWidth(0, 15 * 112);
        //------列-------
        CellRangeAddress cellRangeAddress1 = new CellRangeAddress(1, 1, 1, 1);
        sheet.addMergedRegion(cellRangeAddress1);
        HSSFCell cell2 = row.createCell(1);
        cell2.setCellValue("任务单号");
        cell2.setCellStyle(style);
        sheet.setColumnWidth(1, 15 * 512);
        //------列-------
        CellRangeAddress cellRangeAddress2 = new CellRangeAddress(1, 1, 2, 2);
        sheet.addMergedRegion(cellRangeAddress2);
        HSSFCell cell3 = row.createCell(2);
        cell3.setCellValue("任务名称");
        cell3.setCellStyle(style);
        sheet.setColumnWidth(2, 15 * 112);
        //------列-------
        CellRangeAddress cellRangeAddress3 = new CellRangeAddress(1, 1, 3, 3);
        sheet.addMergedRegion(cellRangeAddress3);
        HSSFCell cell4 = row.createCell(3);
        cell4.setCellValue("责任人");
        cell4.setCellStyle(style);
        sheet.setColumnWidth(3, 15 * 312);
        //------列-------
        CellRangeAddress cellRangeAddress4 = new CellRangeAddress(1, 1, 4, 4);
        sheet.addMergedRegion(cellRangeAddress4);
        HSSFCell cell5 = row.createCell(4);
        cell5.setCellValue("开始日期");
        cell5.setCellStyle(style);
        sheet.setColumnWidth(4, 15 * 312);
        //------列-------
        CellRangeAddress cellRangeAddress5 = new CellRangeAddress(1, 1, 5, 5);
        sheet.addMergedRegion(cellRangeAddress5);
        HSSFCell cell6 = row.createCell(5);
        cell6.setCellValue("结束日期");
        cell6.setCellStyle(style);
        sheet.setColumnWidth(5, 15 * 200);
        //------列-------
        CellRangeAddress cellRangeAddress6 = new CellRangeAddress(1, 1, 6, 6);
        sheet.addMergedRegion(cellRangeAddress6);
        HSSFCell cell7 = row.createCell(6);
        cell7.setCellValue("计划开始时间");
        cell7.setCellStyle(style);
        sheet.setColumnWidth(6, 15 * 200);
        //------列-------
        CellRangeAddress cellRangeAddress7 = new CellRangeAddress(1, 1, 7, 7);
        sheet.addMergedRegion(cellRangeAddress7);
        HSSFCell cell8 = row.createCell(7);
        cell8.setCellValue("计划结束时间");
        cell8.setCellStyle(style);
        sheet.setColumnWidth(7, 15 * 282);
        //------列-------
        CellRangeAddress cellRangeAddress8 = new CellRangeAddress(1, 1, 8, 8);
        sheet.addMergedRegion(cellRangeAddress8);
        HSSFCell cell9 = row.createCell(8);
        cell9.setCellValue("到期日期");
        cell9.setCellStyle(style);
        sheet.setColumnWidth(8, 15 * 282);
        //------列-------
        CellRangeAddress cellRangeAddress9 = new CellRangeAddress(1, 1, 9, 9);
        sheet.addMergedRegion(cellRangeAddress9);
        HSSFCell cell10 = row.createCell(9);
        cell10.setCellValue("任务内容");
        cell10.setCellStyle(style);
        sheet.setColumnWidth(9, 15 * 212);
        //------列-------
        CellRangeAddress cellRangeAddress10 = new CellRangeAddress(1, 1, 10, 10);
        sheet.addMergedRegion(cellRangeAddress10);
        HSSFCell cell11 = row.createCell(10);
        cell11.setCellValue("责任单位");
        cell11.setCellStyle(style);
        sheet.setColumnWidth(10, 15 * 212);
        //------列-------
        CellRangeAddress cellRangeAddress11 = new CellRangeAddress(1, 1, 11, 11);
        sheet.addMergedRegion(cellRangeAddress11);
        HSSFCell cell12 = row.createCell(11);
        cell12.setCellValue("责任人手机号");
        cell12.setCellStyle(style);
        sheet.setColumnWidth(11, 15 * 212);
        //------列-------
        CellRangeAddress cellRangeAddress12 = new CellRangeAddress(1, 1, 12, 12);
        sheet.addMergedRegion(cellRangeAddress12);
        HSSFCell cell13 = row.createCell(12);
        cell13.setCellValue("经办人");
        cell13.setCellStyle(style);
        sheet.setColumnWidth(12, 15 * 212);
        //------列-------
        CellRangeAddress cellRangeAddress13 = new CellRangeAddress(1, 1, 13, 13);
        sheet.addMergedRegion(cellRangeAddress13);
        HSSFCell cell14 = row.createCell(13);
        cell14.setCellValue("经办人手机号");
        cell14.setCellStyle(style);
        sheet.setColumnWidth(13, 15 * 252);
        //------列-------
        CellRangeAddress cellRangeAddress14 = new CellRangeAddress(1, 1, 14, 14);
        sheet.addMergedRegion(cellRangeAddress14);
        HSSFCell cell15 = row.createCell(14);
        cell15.setCellValue("任务时间");
        cell15.setCellStyle(style);
        sheet.setColumnWidth(14, 15 * 212);
        //------列-------
        CellRangeAddress cellRangeAddress15 = new CellRangeAddress(1, 1, 15, 15);
        sheet.addMergedRegion(cellRangeAddress15);
        HSSFCell cell16 = row.createCell(15);
        cell16.setCellValue("督查组类别");
        cell16.setCellStyle(style);
        sheet.setColumnWidth(15, 15 * 252);
        //------列-------
        CellRangeAddress cellRangeAddress16 = new CellRangeAddress(1, 1, 16, 16);
        sheet.addMergedRegion(cellRangeAddress16);
        HSSFCell cell17 = row.createCell(16);
        cell17.setCellValue("手机号");
        cell17.setCellStyle(style);
        sheet.setColumnWidth(16, 15 * 282);
        //------列-------
        CellRangeAddress cellRangeAddress17 = new CellRangeAddress(1, 1, 17, 17);
        sheet.addMergedRegion(cellRangeAddress17);
        HSSFCell cell18 = row.createCell(17);
        cell18.setCellValue("佐证材料");
        cell18.setCellStyle(style);
        sheet.setColumnWidth(17, 15 * 282);

*/

        int rows = 0;
        for (int i = 0; i < dataList.size(); i++) {
            rows++;
            row = sheet.createRow(rows);
            row.createCell(0).setCellValue(rows);
            for (int m = 0 ; m<heads.size();m++)
            {
                if("sysId".equals(heads.get(m))){
                    row.createCell(m+1).setCellValue(  dataList.get(i).getSysId()  );
                }
                if("taskName".equals(heads.get(m)))
               row.createCell(m+1).setCellValue(dataList.get(i).getTaskName());
                if("resUser".equals(heads.get(m)))
               row.createCell(m+1).setCellValue(dataList.get(i).getResUser());
                if("startDate".equals(heads.get(m)))
               row.createCell(m+1).setCellValue(dataList.get(i).getStartDate());
                if("endDate".equals(heads.get(m)))
               row.createCell(m+1).setCellValue(dataList.get(i).getEndDate());
                if("planStartDate".equals(heads.get(m)))
               row.createCell(m+1).setCellValue(dataList.get(i).getPlanStartDate());
                if("planStartDate".equals(heads.get(m)))
               row.createCell(m+1).setCellValue(dataList.get(i).getPlanEndDate());
                if("expirationDate".equals(heads.get(m)))
               row.createCell(m+1).setCellValue(dataList.get(i).getExpirationDate());
                if("taskContent".equals(heads.get(m)))
               row.createCell(m+1).setCellValue(dataList.get(i).getTaskContent());
                if("resDept".equals(heads.get(m)))
                row.createCell(m+1).setCellValue(dataList.get(i).getResDept());
                if("resPhone".equals(heads.get(m)))
                row.createCell(m+1).setCellValue(dataList.get(i).getResPhone());
                if("operatorUser".equals(heads.get(m)))
                row.createCell(m+1).setCellValue(dataList.get(i).getOperatorUser());
                if("operatorPhone".equals(heads.get(m)))
                row.createCell(m+1).setCellValue(dataList.get(i).getOperatorPhone());
                if("taskDate".equals(heads.get(m)))
                row.createCell(m+1).setCellValue(dataList.get(i).getTaskDate());
                if("groupCode".equals(heads.get(m)))
                row.createCell(m+1).setCellValue(dataList.get(i).getGroupCode());
                if("phones".equals(heads.get(m)))
                row.createCell(m+1).setCellValue(dataList.get(i).getPhones());
                if("taxproof".equals(heads.get(m)))
                row.createCell(m+1).setCellValue(dataList.get(i).getTaxproof());
            }



        }
        return wb;
    }

}
